package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.annotation.SystemServiceLog;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootBfDetail;
import org.footballmanager.pandafootball.system.domain.FootBfinfo;
import org.footballmanager.pandafootball.system.form.impl.FootBfDetailSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootBfDetailMapper;
import org.footballmanager.pandafootball.system.service.FootBfDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class FootBfDetailServiceImpl extends ServiceImpl<FootBfDetail> implements FootBfDetailService {
    public static final String JQSUFFIX = "BFJQTEMP";
    public static final String ZGSUFFIX = "BFZGTEMP";
    @Autowired
    private FootBfDetailMapper footBfDetailMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 根据条件获取报分进球及助攻记录列表
     *
     * @param entityname             实体类名
     * @param map                    参数
     * @param footBfDetailSearchForm 条件
     */
    @Override
    @SystemServiceLog(description = "根据条件获取报分进球及助攻记录列表")
    public void search(String entityname, Model map, FootBfDetailSearchForm footBfDetailSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footBfDetailSearchForm);
        Page<FootBfDetail> page = PageHelper.startPage(footBfDetailSearchForm.getPageIndex(), footBfDetailSearchForm.getPageSize());
        List<FootBfDetail> list = this.findall(footBfDetailSearchForm.buildSearchCommond());
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 根据条件删除报分进球及助攻记录列表
     *
     * @param footBfDetailSearchForm 条件
     * @param request                请求
     */
    @Override
    @SystemServiceLog(description = "根据条件删除报分进球及助攻记录列表")
    public void deleteAll(FootBfDetailSearchForm footBfDetailSearchForm, HttpServletRequest request) {
        if (footBfDetailSearchForm.getSelectIds() != null) {
            String[] result = footBfDetailSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 根据报分主键获取进球及助攻记录
     *
     * @param bfId 报分主键
     * @return 详情记录列表
     */
    @Override
    @SystemServiceLog(description = "根据报分主键获取进球及助攻记录")
    public List<FootBfDetail> findByBfId(Long bfId) {
        Example example = new Example(FootBfDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bfId", bfId);
        criteria.andEqualTo("delFlag", 0);
        return this.findall(example);
    }

    /**
     * 根据报分主键和进球球员关系主键查找记录数量
     *
     * @param bfId    报分主键
     * @param jqFupId 进球球员关系主键
     * @return 记录数量
     */
    @Override
    @SystemServiceLog(description = "根据报分主键和进球球员关系主键查找记录数量")
    public Integer findByBfIdAndJqId(Long bfId, Long jqFupId) {
        Example example = new Example(FootBfDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bfId", bfId);
        criteria.andEqualTo("fupId", jqFupId);
        criteria.andEqualTo("type", 1);
        criteria.andEqualTo("delFlag", 0);
        return this.countByCondition(example);
    }

    /**
     * 根据报分主键和进球球员关系主键查找记录数量
     *
     * @param bfId    报分主键
     * @param zgFupId 助攻球员关系主键
     * @return 记录数量
     */
    @Override
    @SystemServiceLog(description = "根据报分主键和进球球员关系主键查找记录数量")
    public Integer findByBfIdAndZgId(Long bfId, Long zgFupId) {
        Example example = new Example(FootBfDetail.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("bfId", bfId);
        criteria.andEqualTo("fupId", zgFupId);
        criteria.andEqualTo("type", 2);
        criteria.andEqualTo("delFlag", 0);
        return this.countByCondition(example);
    }

    /**
     * 保存进球及助攻记录到redis
     *
     * @param team    球队主键
     * @param type    类型
     * @param ids     球员关系主键
     * @param nums    对应类型的数量
     * @param request 请求
     */
    @Override
    @SystemServiceLog(description = "保存进球及助攻记录到redis")
    public void saveEntityTemp(Long team, Integer type, String ids, String nums, HttpServletRequest request) {
        List<FootBfDetail> list = new ArrayList<>();
        if (team != null && !StringUtils.isEmpty(ids) && !StringUtils.isEmpty(nums)) {
            String[] idsStr = ids.split(Constants.COMMA);
            String[] numsStr = nums.split(Constants.COMMA);
            for (int i = 0; i < numsStr.length; i++) {
                if (!numsStr[i].equals("0")) {
                    FootBfDetail detail = new FootBfDetail();
                    detail.setFupId(Long.parseLong(idsStr[i]));
                    detail.setType(type);
                    list.add(detail);
                }
            }
        }
        if (!list.isEmpty()) {
            if (type == 1) {
                redisService.set(JQSUFFIX + team, list);
            } else {
                redisService.set(ZGSUFFIX + team, list);
            }
        }
    }

    /**
     * 保存进球及助攻记录到数据库
     *
     * @param footBfinfo 报分实体类
     * @param request    请求
     */
    @Override
    @SystemServiceLog(description = "保存进球及助攻记录到数据库")
    public void saveEntity(FootBfinfo footBfinfo, HttpServletRequest request) {
        List<FootBfDetail> zhuList = (List<FootBfDetail>) redisService.get(JQSUFFIX + footBfinfo.getQd1());
        List<FootBfDetail> keList = (List<FootBfDetail>) redisService.get(JQSUFFIX + footBfinfo.getQd2());
        if (zhuList != null) {
            saveRecord(zhuList, footBfinfo.getId(), request);
        }
        if (keList != null) {
            saveRecord(keList, footBfinfo.getId(), request);
        }
        zhuList = (List<FootBfDetail>) redisService.get(ZGSUFFIX + footBfinfo.getQd1());
        keList = (List<FootBfDetail>) redisService.get(ZGSUFFIX + footBfinfo.getQd2());
        if (zhuList != null) {
            saveRecord(zhuList, footBfinfo.getId(), request);
        }
        if (keList != null) {
            saveRecord(keList, footBfinfo.getId(), request);
        }
    }

    private void saveRecord(List<FootBfDetail> details, Long bfId, HttpServletRequest request) {
        if (details != null && !details.isEmpty()) {
            for (FootBfDetail detail : details) {
                detail.setBfId(bfId);
                this.save(detail, request);
            }
        }
    }

    /**
     * 射手榜
     *
     * @return 射手榜
     */
    @Override
    @SystemServiceLog(description = "获取射手榜")
    public List<FootBfDetail> findJqTop10() {
        return footBfDetailMapper.findTopByType(1);
    }

    /**
     * 助攻榜
     *
     * @return
     */
    @Override
    @SystemServiceLog(description = "获取助攻榜")
    public List<FootBfDetail> findZgTop10() {
        return footBfDetailMapper.findTopByType(2);
    }
}
